package com.lock.immersive.controller;

import com.lock.immersive.annotation.AuthCheck;
import com.lock.immersive.exception.BusinessException;
import com.lock.immersive.mapper.TuanMapper;
import com.lock.immersive.pojo.domain.Tuan;
import com.lock.immersive.pojo.dto.TuanDto;
import com.lock.immersive.pojo.dto.UserTuanDto;
import com.lock.immersive.pojo.vo.ProductItemVo;
import com.lock.immersive.service.TuanService;
import com.lock.immersive.utils.ErrorCode;
import com.lock.immersive.utils.ResponseApi;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/tuan")
public class TuanController {
    @Resource
    public TuanService tuanService;

    @Resource
    public TuanMapper tuanMapper;

    @PostMapping("/createTuan")
    @AuthCheck(mustRole = "superManager")
    public ResponseApi<Object> cereateTuan(@RequestBody TuanDto tuanDto, HttpServletRequest request) {
        return tuanService.createTuan(tuanDto, request);
    }

    @PostMapping("/updateTuan")
    @AuthCheck(mustRole = "superManager")
    public ResponseApi<Object> updateTuan(@RequestBody Tuan tuan, HttpServletRequest request) {
        return tuanService.updateTuan(tuan, request);
    }

    @GetMapping("/getProductById")
    public ResponseApi<ProductItemVo> getProductById(@RequestParam("productId") Integer productId, HttpServletRequest request) {
        return tuanService.getProductById(productId, request);
    }

    @PostMapping("/joinTuan")
    @AuthCheck
    public ResponseApi<Integer> joinTuan(@RequestBody UserTuanDto userTuanDto, HttpServletRequest request) {
        return tuanService.joinTuan(userTuanDto, request);
    }

    @PostMapping("/deleteTuanList")
    @AuthCheck
    public ResponseApi<Object> deleteTuan(@RequestBody List<String> ids) {
        int i = tuanMapper.deleteBatchIds(ids);
        if (i > 0) {
            return ResponseApi.success("删除成功");
        } else {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
    }
    
}
